﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Website.UserAddress;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Website.UserAddress;

namespace BCTMSRESTful.Controllers.Anonymous
{
    /// <summary>
    /// 用户地址
    /// </summary>
    [Route("api/v1/[controller]")]
    public class UserAddressController : WebsiteBaseController
    {
        private readonly IUserAddressService userAddressService;

        public UserAddressController(IUserAddressService userAddressService)
        {
            this.userAddressService = userAddressService;
        }

        #region 添加用户地址
        /// <summary>
        ///  添加用户地址
        /// </summary>
        /// <param name="requestDto">用户地址信息</param>
        /// <response code="201">添加用户地址成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加用户地址")]
        [ProducesResponseType(typeof(UserAddressDto), 201)]
        public IActionResult Post(
            [FromBody]UserAddressRequestDto requestDto)
        {
            var result = this.userAddressService.Add(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改用户地址
        /// <summary>
        ///  修改用户地址
        /// </summary>
        /// <param name="userAddressId">用户地址Id</param>
        /// <param name="requestDto">用户地址信息</param>
        /// <response code="200">修改用户地址成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{user_address_id}")]
        [Log("修改用户地址")]
        [ProducesResponseType(typeof(UserAddressDto), 200)]
        public IActionResult Put([FromRoute(Name = "user_address_id")]long userAddressId, [FromBody]UserAddressRequestDto requestDto)
        {
            UserAddressPutDto putDto = requestDto.As<UserAddressPutDto>();
            putDto.UserAddressId = userAddressId;
            return StatusCode((int)HttpStatusCode.OK, this.userAddressService.Update(putDto));
        }
        #endregion

        #region 设置默认用户地址
        /// <summary>
        ///  设置默认用户地址
        /// </summary>
        /// <param name="userAddressId">用户地址Id</param>
        /// <response code="200">修改用户地址成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{user_address_id}")]
        [Log("设置默认用户地址")]
        [ProducesResponseType(typeof(bool), 204)]
        public IActionResult Default([FromRoute(Name = "user_address_id")]long userAddressId)
        {
            this.userAddressService.SettingIsDefault(userAddressId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion
        

        #region 删除用户地址
        /// <summary>
        ///  删除用户地址
        /// </summary>
        /// <param name="userAddressId">用户地址Id</param>
        /// <response code="204">删除用户地址成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{user_address_id}")]
        [Log("删除用户地址")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "user_address_id")]long userAddressId)
        {
            this.userAddressService.Delete(userAddressId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取用户地址
        /// <summary>
        ///  获取用户地址
        /// </summary>
        /// <param name="userAddressId">用户地址Id</param>
        /// <response code="200">获取用户地址</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{user_address_id}")]
        [ProducesResponseType(typeof(UserAddressDto), 200)]
        public IActionResult Get([FromRoute(Name = "user_address_id")]long userAddressId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.userAddressService.Get(userAddressId));
        }
        #endregion

        #region 获取用户地址总数
        /// <summary>
        ///  获取用户地址总数
        /// </summary>
        /// <response code="200">获取用户地址总数</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(int), 200)]
        public IActionResult Count()
        {
            return StatusCode((int)HttpStatusCode.OK, this.userAddressService.Count(this.UserInfo.UserId));
        }
        #endregion

        #region 获取用户地址列表
        /// <summary>
        ///  获取用户地址列表
        /// </summary>
        /// <response code="200">获取用户地址列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<UserAddressDto>), 200)]
        public IActionResult Get()
        {
            return StatusCode((int)HttpStatusCode.OK, this.userAddressService.GetList(this.UserInfo.UserId));
        }
        #endregion

        #region 获取用户地址（分页）
        /// <summary>
        ///  获取用户地址（分页）
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取用户地址（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<UserAddressDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "user_id")]long? userId,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.userAddressService.Query(userId,start, end, sortName, ascending, index, size));
        }
        #endregion
    }
}

